package cn.jszx.core.controller.admin;

import cn.jszx.core.bean.user.User;
import cn.jszx.core.service.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by karakapi on 15-9-15.
 */
@Controller
@RequestMapping(value = "/control/user")
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    UserService userservice;
    //进入用户列表
    @RequestMapping(value = "/main",method = RequestMethod.GET)
    public String main() {
        logger.debug("=> usermain");
        return "user/user_main";
    }
    //查询用户
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public String search(User user) {
        logger.debug("=> user search");
        return "redirect:main";
    }

    //创建用户页面
    @RequestMapping(value = "/create",method = RequestMethod.GET)
    public String createPage() {
        logger.debug("=> user createPage");
        return "user/user_create";
    }

    //创建用户
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String create(User user) {
        logger.debug("=> user create " + user);
        userservice.addUser(user);
        return "redirect:main";
    }

    //编辑用户页面
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String editPage(ModelMap model) {
        logger.debug("=> user editPage");
        //前台页面显示
        User user=userservice.getUserByKey("1");
        logger.debug(user.toString());
        model.addAttribute("user",userservice.getUserByKey("1"));
        return "user/user_edit";
    }

    //编辑用户
    @RequestMapping(value = "/edit",method = RequestMethod.PUT)
    public String edit(User user) {
        logger.debug("=> user edit "+user);
        userservice.updateUserByKey(user);
        return "redirect:main";
    }

    //删除用户
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public String delete(Long[] ids) {
        logger.debug("=> user delete  "+ids);
        return "redirect:main";
    }




}
